package body;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
	static Connection con;
	public static Statement st;
	static PreparedStatement ps;
	public static ResultSet rs;
	static String url="jdbc:mysql://localhost:3306/studb?useUnicode=true&characterEncoding=UTF-8";
	static String name="root";
	static String pwd="123456";
	public static void initst() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		    con=DriverManager.getConnection(url,name,pwd);
			st=con.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	public static void initps(String sql) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection(url,name,pwd);
			ps=con.prepareStatement(sql);
			rs= ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	public static void closeDB() {
		try {
			if (rs!=null) {
				rs.close();
			}
			if (ps!=null) {
				ps.close();
			} 
			if (st!=null) {
				st.close();
			} 
			if (con!=null) {
				con.close();
			} 
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
